package ai.tasks;

import ai.AIConstant;
import ai.Trackable;
import ai.PathFinding;
import ai.AIUtil;
import ai.engine.AIUnit;
import ai.engine.Task;
import ai.engine.TaskDoer;
import ai.engine.factories.TaskFactory;
import engine.GameEngine;
import engine.UnitAction;
import engine.data.UnitType;
import util.List;

public class RunAwayTask extends Task {

	public RunAwayTask(short id, Trackable target) {
		super(id, TaskFactory.RUN_AWAY_TASK, AIUnit.class, "Run Away", "",
				AIConstant.PRIORITY_MEDIUM, target, AIUtil.getCurrentAIP().runAwayMin,
		AIUtil.getCurrentAIP().runAwayMax);
	}

	public AIUnit getTarget() {
		return (AIUnit) target;
	}
	
	public boolean isTaskSuitable(TaskDoer possibleTaskDoer) {
		AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return PathFinding.getHeuristic(possibleUnitDoer, this.getTarget()) < (AIConstant.DISTANCE_RATIO * 2 * UnitType
				.getMP(this.getTarget().type));
	}

	public short getFormula(TaskDoer possibleTaskDoer) {
		AIUnit possibleUnitDoer = (AIUnit) possibleTaskDoer;
		return (short) (this.priority + (AIUtil.getCurrentAIP().carefulness
				+ UnitType.getAP(this.getTarget().type) - UnitType
				.getDP(possibleUnitDoer.type)));
	}

	public byte getAction(TaskDoer taskDoer) {
		AIUnit unitDoer = (AIUnit) taskDoer;
		if (!this.assignedResources.contains(unitDoer))
			return UnitAction.SKIP;
		// Trouver le chemin et avancer d'un pas dans le chemin.
		short[] lastSeenPosition = this.getTarget().getLastSeenPosition(
				GameEngine.currPlayer);
		return PathFinding.getNextDirection(PathFinding.TYPE_ESCAPE,
				unitDoer, lastSeenPosition);
	}

	public List getAdditionalResourcesNeeded() {
		return null;
	}
	
	public boolean isObjectiveAchieved() {
		return true; // fuit un tour et puis voila
	}

}
